﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

using SB.Logging;
using SB.Tools;
using SB.UserSettings;

namespace SB.CustomControlLibrary
{
    public partial class UserDataGridViewCustomize : Form
    {
        private static readonly ILogger log = LoggingFacade.GetLogger(typeof(UserDataGridViewCustomize));

        private SortableBindingList<UserSetting> angezeigte = new SortableBindingList<UserSetting>();
        private SortableBindingList<UserSetting> nichtAngezeigte = new SortableBindingList<UserSetting>();

        private string uniqueDatagridName;
        private Type entityType;

        public UserDataGridViewCustomize(string nameChild, Type t)
        {
            InitializeComponent();

            this.uniqueDatagridName = nameChild;
            this.entityType = t;

            Text = "Anzeige von " + nameChild + " anpassen";

            InitializeData(entityType);

            lstDisplayed.DataSource = angezeigte;
            lstDisplayed.DisplayMember = "Text";

            lstNotDisplayed.DataSource = nichtAngezeigte;
            lstNotDisplayed.DisplayMember = "Name";
        }

        public static SortableBindingList<UserSetting> ResetColumns(Type type, string uniqueDatagridName)
        {
            SortableBindingList<UserSetting> settings = UserDataGridViewCustomize.getAllColumns(type);
            UserSettingsFacade.XMLInterface.WriteUserSettings(settings, UserSettingsFacade.WriteTypes.Overwrite, true, "UserDataGridViews", uniqueDatagridName);
            return settings;
        }

        public static SortableBindingList<UserSetting> getAllColumns(Type type)
        {
            SortableBindingList<UserSetting> list = new SortableBindingList<UserSetting>();
            PropertyInfo[] properties = type.GetProperties();

            foreach (var prop in properties)
            {
                list.Add(new UserSetting(prop.Name, prop.Name));
            }

            return list;
        }

        private void InitializeData(Type type)
        {
            nichtAngezeigte = getAllColumns(type);

            // Alle angezeigten lesen
            SortableBindingList<UserSetting> tmp = UserSettingsFacade.XMLInterface.ReadUserSettings("UserDataGridViews", uniqueDatagridName);

            foreach (UserSetting s in tmp)
                angezeigte.Add(s);
            //TLCommons.ReadDatagridColumnsFromXML(ref angezeigte, nameChild);

            // Alle entfernen die nicht als Property aufscheinen
            List<UserSetting> toRemove = new List<UserSetting>();
            foreach (UserSetting s in angezeigte)
            {
                if (!nichtAngezeigte.Contains(s))
                    toRemove.Add(s);
            }

            foreach (UserSetting s in toRemove)
                angezeigte.Remove(s);

            // Alle nicht angezeigten entfernen die in der angezeigten Liste vorkommen
            toRemove.Clear();
            foreach (UserSetting s in nichtAngezeigte)
            {
                if (angezeigte.Contains(s))
                    toRemove.Add(s);
            }

            foreach (UserSetting s in toRemove)
                nichtAngezeigte.Remove(s);
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (lstNotDisplayed.SelectedIndex >= 0)
            {
                UserSetting prop = (UserSetting)lstNotDisplayed.SelectedItem;
                nichtAngezeigte.Remove(prop);
                angezeigte.Add(prop);

                lstDisplayed.SelectedItem = prop;
            }
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            if (lstDisplayed.SelectedIndex >= 0)
            {
                UserSetting prop = (UserSetting)lstDisplayed.SelectedItem;
                angezeigte.Remove(prop);
                nichtAngezeigte.Add(prop);

                lstNotDisplayed.SelectedItem = prop;
            }
        }

        private void btnMoveUp_Click(object sender, EventArgs e)
        {
            if (lstDisplayed.SelectedIndex > 0)
            {
                UserSetting prop = (UserSetting)lstDisplayed.SelectedItem;
                int pos = angezeigte.IndexOf(prop);
                angezeigte.Remove(prop);
                angezeigte.Insert(pos - 1, prop);
                lstDisplayed.SelectedItem = prop;
            }
        }

        private void btnMoveDown_Click(object sender, EventArgs e)
        {
            if (lstDisplayed.SelectedIndex < angezeigte.Count-1)
            {
                UserSetting prop = (UserSetting)lstDisplayed.SelectedItem;
                int pos = angezeigte.IndexOf(prop);
                angezeigte.Remove(prop);
                angezeigte.Insert(pos + 1, prop);
                lstDisplayed.SelectedItem = prop;
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            UserSettingsFacade.XMLInterface.WriteUserSettings(angezeigte, UserSettingsFacade.WriteTypes.Overwrite, true, "UserDataGridViews", uniqueDatagridName);
        }

        private void btnRename_Click(object sender, EventArgs e)
        {
            if (lstDisplayed.SelectedIndex >= 0)
            {
                UserSetting prop = (UserSetting)lstDisplayed.SelectedItem;
                if (new TicketLineDatagridRename(prop).ShowDialog() == DialogResult.OK)
                {
                    int pos = angezeigte.IndexOf(prop);
                    angezeigte.Remove(prop);
                    angezeigte.Insert(pos, prop);
                    lstDisplayed.SelectedItem = prop;
                }
            }
        }

        private void btnStandard_Click(object sender, EventArgs e)
        {
            ResetColumns(entityType, uniqueDatagridName);

            angezeigte.Clear();
            nichtAngezeigte.Clear();

            InitializeData(entityType);
        }
    }
}
